package com.yf.modules.security.handler;

import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @author chentianwei
 * @date 2023/4/24 13:28
 * @description Jwt拒绝访问处理
 */
@Component
public class JwtAccessDeniedHandler implements AccessDeniedHandler {

    @Override
    public void handle(HttpServletRequest httpServletRequest,
                       HttpServletResponse httpServletResponse,
                       AccessDeniedException e) throws IOException, ServletException {

        //当被拒绝访问时，抛出403拒绝访问异常
        httpServletResponse.sendError(HttpServletResponse.SC_FORBIDDEN,e.getMessage());
    }
}
